package math;

import java.util.Date;
import java.util.List;

import io.GPSPoint;

public class Distance {

	private static final double D2R = Math.PI / 180.0;
	
	public static double haversine(double lat1, double long1, double lat2, double long2) {
	    double dlong = (long2 - long1) * D2R;
	    double dlat = (lat2 - lat1) * D2R;
	    double a = Math.pow(Math.sin(dlat/2.0), 2) + Math.cos(lat1*D2R) * Math.cos(lat2*D2R) * Math.pow(Math.sin(dlong/2.0), 2);
	    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
	    double d = 6367000 * c;
	    return d;
	}
	
	public static double distMeter(GPSPoint p1, GPSPoint p2) {
		return haversine(p1.getLatti(), p1.getLongi(), p2.getLatti(), p2.getLongi());
	}
	
	public static double getTotalDist(List<GPSPoint> points) {
		double d = 0;
		if (points.size() == 0) {
			return 0;
		}
		for (int i = 1; i < points.size(); i++) {
			d += distMeter(points.get(i - 1), points.get(i));
		}
		return d;
	}

	public static double getTotalDist(List<GPSPoint> points, long[] startStop) {
		double d = 0;
		if (points.size() == 0) {
			return 0;
		}
		for (int i = 1; i < points.size(); i++) {
			if (startStop[0] < points.get(i-1).getTime() && points.get(i).getTime() < startStop[1] ) {
				d += distMeter(points.get(i - 1), points.get(i));
			}
		}
		return d;
	}

}
